import 'dart:io';

///"baseUrl": "temp/43234324.jpg",
///"visitUrl": "https://test-store-bucket.oss-cn-hangzhou.aliyuncs.com/20190301/43234324.jpg"
class FileUploadBean {
  String? relativePath;
  String? url;
  String? localPath;
  bool? _cleared;

  FileUploadBean({
    this.relativePath,
    this.url,
    this.localPath,
  });

  FileUploadBean.fromUrl(String url) {
    this.url = url;
  }

  FileUploadBean.fromFile(File file) {
    this.localPath = file.path;
  }

  FileUploadBean.fromPath(String filePath) {
    this.localPath = filePath;
  }

  FileUploadBean.fromJson(Map<String, dynamic> json) {
    this.relativePath = json['baseUrl'];
    this.url = json['visitUrl'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['baseUrl'] = this.relativePath;
    data['visitUrl'] = this.url;
    return data;
  }

  bool get hasLocalCache =>
      _cleared != true &&
      localPath != null &&
      localPath!.isNotEmpty &&
      File(localPath!).existsSync();

  bool get hasUrl => _cleared != true && (url?.startsWith("http") ?? false);

  bool get isEmpty =>
      _cleared == true ||
      (url?.isEmpty ?? true) && (localPath?.isEmpty ?? true);

  void clear() {
    _cleared = true;
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is FileUploadBean &&
          runtimeType == other.runtimeType &&
          (localPath != null && localPath == other.localPath ||
              url != null && url == other.url ||
              relativePath != null && relativePath == other.relativePath);

  @override
  int get hashCode => localPath?.hashCode ?? 0;

  @override
  String toString() {
    return 'ApiFileUploadBean{\nbaseUrl: $relativePath, \nvisitUrl: $url, \nlocalPath: $localPath}';
  }
}
